package com.ruoyi.info.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.StringUtils;
import com.ruoyi.info.mapper.InfoStuMapper;
import com.ruoyi.info.domain.InfoStu;
import com.ruoyi.info.service.IInfoStuService;

import java.util.List;

/**
 * 学生Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-23
 */
@Service
public class InfoStuServiceImpl extends ServiceImpl<InfoStuMapper, InfoStu> implements IInfoStuService {

    private final InfoStuMapper infoStuMapper;

    public InfoStuServiceImpl(InfoStuMapper infoStuMapper) {
        this.infoStuMapper = infoStuMapper;
    }

    @Override
    public List<InfoStu> queryList(InfoStu infoStu) {
        LambdaQueryWrapper<InfoStu> lqw = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(infoStu.getStuNo())){
            lqw.eq(InfoStu::getStuNo ,infoStu.getStuNo());
        }
        if (StringUtils.isNotBlank(infoStu.getStuName())){
            lqw.like(InfoStu::getStuName ,infoStu.getStuName());
        }
        if (StringUtils.isNotBlank(infoStu.getClassCode())){
            lqw.eq(InfoStu::getClassCode ,infoStu.getClassCode());
        }
        return this.list(lqw);
    }

    @Override
    public String importStu(List<InfoStu> stuList, Boolean isUpdateSupport, String operName){
        if (com.ruoyi.common.utils.StringUtils.isNull(stuList) || stuList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (InfoStu stu : stuList)
        {
            try
            {
                QueryWrapper<InfoStu> qw = new QueryWrapper<>();
                qw.eq("stu_no", stu.getStuNo());
                // 验证是否存在这个用户
                InfoStu u = infoStuMapper.selectOne(qw);
                if (com.ruoyi.common.utils.StringUtils.isNull(u))
                {
                    infoStuMapper.insert(stu);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、学生 " + stu.getStuName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    stu.setId(u.getId());
                    infoStuMapper.updateById(stu);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、学生 " + stu.getStuName()  + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、学生 " + stu.getStuName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、学生 " + stu.getStuName()  + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}